

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using JTC.MiniCommerce.Core;
using JTC.MiniCommerce.Localization;

namespace JTC.MiniCommerce.Web.admin {

  public partial class rolelist : System.Web.UI.Page {

    #region Constants

    private const string REDIRECT_PAGE = "~/admin/rolelist.aspx";
    private const string LB_DELETE = "lbDelete";
    private const string CMD_MYDELETE = "mydelete";

    #endregion

    #region Page Events

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e) {
      try {
        string[] roles = Roles.GetAllRoles();
        dgRoles.DataSource = roles;
        dgRoles.ItemDataBound += new DataGridItemEventHandler(dgRoles_ItemDataBound);
        dgRoles.DataBind();
        SetRoleListProperties();
      }
      catch (Exception ex) {
        Logger.Error(typeof(rolelist).Name + ".Page_Load", ex);
        Master.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }
    }

    /// <summary>
    /// Handles the Click event of the lbDelete control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
    protected void lbDelete_Click(object sender, CommandEventArgs e) {
      try {
        if (e.CommandName.Equals(CMD_MYDELETE)) {
          string roleName = e.CommandArgument as string;
          Roles.DeleteRole(roleName);
          Response.Redirect(REDIRECT_PAGE);
        }
      }
      catch (Exception ex) {
        Logger.Error(typeof(rolelist).Name + ".lbDelete_Click", ex);
        Master.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }
    }

    /// <summary>
    /// Handles the Click event of the btnSave control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void btnSave_Click(object sender, EventArgs e) {
      if (Page.IsValid) {
        try {
          Roles.CreateRole(txtRoleName.Text.Trim());
          Response.Redirect(REDIRECT_PAGE);
        }
        catch (Exception ex) {
          Logger.Error(typeof(rolelist).Name + ".btnSave_Click", ex);
          Master.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
        }
      }
    }

    /// <summary>
    /// Handles the ItemDataBound event of the dgRoles control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.Web.UI.WebControls.DataGridItemEventArgs"/> instance containing the event data.</param>
    private void dgRoles_ItemDataBound(object sender, DataGridItemEventArgs e) {
      if (e.Item.ItemType == ListItemType.Header) { //TODO: Move these to after setting the datasource
        e.Item.Cells[0].Text = LocalizationUtility.GetText("hdrDelete");
        e.Item.Cells[1].Text = LocalizationUtility.GetText("hdrRoleName");
      }
      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
        LinkButton linkButton = e.Item.Cells[0].FindControl(LB_DELETE) as LinkButton;
        if (linkButton != null) {
          linkButton.Text = LocalizationUtility.GetText("lblDelete");
        }
        e.Item.Attributes.Add("onmouseover", "this.className = \"over\";");
        e.Item.Attributes.Add("onmouseout", "this.className = this.className.replace(\"over\", \"\");");
      }
    }

    #endregion

    #region Methods

    #region Private

    /// <summary>
    /// Sets the role list properties.
    /// </summary>
    private void SetRoleListProperties() {
      pnlRoles.GroupingText = LocalizationUtility.GetText("pnlRoleList");
      LocalizationUtility.AddPanelText(pnlAddRole);
      LocalizationUtility.AddLabelText(lblRoleName);
      LocalizationUtility.AddButtonText(btnSave);
      LocalizationUtility.AddLabelText(lblRoleAdministration);
      Page.Title = LocalizationUtility.GetText("titleRoleAdministration");
    }

    #endregion

    #endregion

  }
}
